import { Component } from "@angular/core";
import { AdMobFree, AdMobFreeBannerConfig } from "@ionic-native/admob-free/ngx";
import { SplashScreen } from "@ionic-native/splash-screen/ngx";
import { StatusBar } from "@ionic-native/status-bar/ngx";
import { Platform } from "@ionic/angular";
import { EventsService } from "./events.service";
import { Global } from "./global.service";
import { LocalStorage } from "./local-storage.service";

@Component({
	selector: "app-root",
	templateUrl: "app.component.html",
	styleUrls: ["app.component.scss"]
})
export class AppComponent {
	constructor(
		private platform: Platform,
		private splashScreen: SplashScreen,
		private statusBar: StatusBar,
		private global: Global,
		private events: EventsService,
		private adMobFree: AdMobFree,
		private localStorage: LocalStorage
	) {
		this.initializeApp();
		this.events.on(Global.IAP, async () => {
			await this.adMobFree.banner.hide();
		});
	}

	initializeApp() {
		this.platform.ready().then(() => {
			this.statusBar.styleDefault();
			this.splashScreen.hide();
			this.configAdBannerMob();
		});

		if (!this.localStorage.getData(Global.FIRST_LAUNCH)) {
			const date = new Date();
			this.localStorage.setData(Global.FIRST_LAUNCH, {
				date: date.getTime() + 60 * 3600 * 1000
			});
		}
	}

	private async configAdBannerMob() {
		if (this.global.needShowAd) {
			const bannerConfig: AdMobFreeBannerConfig = {
				id: Global.BANNER_ID,
				isTesting: false,
				autoShow: true
			};
			this.adMobFree.banner.config(bannerConfig);
			this.adMobFree.banner.prepare().then().catch(e => console.log(e));
		}
	}




}
